package com.topcheer.provider.rbac.rest;
import java.util.List;
import java.util.Map;

import com.topcheer.common.api.base.vo.BaseUser;
import com.topcheer.provider.rbac.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.topcheer.provider.rbac.entity.Role;
import com.topcheer.provider.rbac.service.RoleService;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.pagehelper.PageInfo;
import com.topcheer.common.base.util.JsonMapper;
import com.topcheer.common.base.vo.BaseResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/role")
@Api(description = "角色信息")
public class RoleController{

    @Autowired
    RoleService roleService;

    @Autowired
    PermissionService permissionService;

    @PostMapping("")
    @ApiOperation(value="新增角色", notes="新增角色", produces="application/json")
    public BaseResponse<Role> insert(@RequestBody Role entity){
        roleService.insert(entity);
        return new BaseResponse<Role>().code(HttpStatus.OK.value()).msg(HttpStatus.OK.getReasonPhrase());
    }

    @GetMapping("/{id}")
    @ApiOperation(value="根据id获取角色", notes="根据id获取角色", produces="application/json")
    public BaseResponse<Role> selectOne(@PathVariable("id") Long id){
        Object o = roleService.selectOne(id);
        return new BaseResponse<Role>().data((Role) o);
    }

    @GetMapping("/page")
    @ApiOperation(value="获取角色分页信息", notes="获取角色分页信息", produces="application/json")
    public BaseResponse<PageInfo> page(String page){
        JsonMapper jsonMapper = JsonMapper.defaultMapper();
        Map map = jsonMapper.fromJson(page, Map.class);
        PageInfo pageInfo = roleService.selectPage(map);
        return new BaseResponse<PageInfo>().data(pageInfo);
    }

    @PutMapping("")
    @ApiOperation(value="更新角色", notes="更新角色", produces="application/json")
    public BaseResponse<Role> update(@RequestBody Role entity){
        roleService.update(entity);
        return new BaseResponse<Role>();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value="删除角色", notes="删除角色", produces="application/json")
    public BaseResponse<Role> delete(@PathVariable("id") Long id){
        roleService.deleteById(id);
        return new BaseResponse<Role>();
    }

    @GetMapping("/all")
    @ApiOperation(value="获取所有角色", notes="获取所有角色", produces="application/json")
    public BaseResponse<List> selectAll(){
        List list = roleService.selectAll();
        return new BaseResponse<List>().data(list);
    }

    @GetMapping("/permission/{loginName}")
    @ApiOperation(value="根据登录名获取权限信息", notes="根据登录名获取权限信息", produces="application/json")
    public BaseResponse<BaseUser> permission(@PathVariable("loginName") String loginName){
        return new BaseResponse<BaseUser>().data(permissionService.getUserInfo(loginName));
    }
}
